<!-- 公告发布管理 -->
 <template>
  <div class="announcements-manage">
    <h3>公告发布管理</h3>

    <!-- 操作按钮 -->
    <div class="actions">
      <el-button type="primary" @click="showAddDialog">新增公告</el-button>
    </div>

    <!-- 筛选查询 -->
    <el-form :inline="true" class="demo-form-inline">
      <el-form-item label="公告标题">
        <el-input v-model="title" placeholder="请输入公告标题"></el-input>
      </el-form-item>
      <el-form-item label="发布时间">
        <el-date-picker v-model="dateRange" type="daterange" range-separator="至" start-placeholder="开始日期" end-placeholder="结束日期"></el-date-picker>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" @click="onSubmit">查询</el-button>
      </el-form-item>
    </el-form>

    <!-- 公告列表 -->
    <el-table :data="tableData" style="width: 100%; margin-top: 20px;">
      <el-table-column prop="title" label="公告标题" width="250"></el-table-column>
      <el-table-column prop="content" label="公告内容" show-overflow-tooltip></el-table-column>
      <el-table-column prop="publishTime" label="发布时间" width="180"></el-table-column>
      <el-table-column prop="status" label="状态" width="100">
        <template #default="scope">
          <el-tag :type="scope.row.status === 'published' ? 'success' : 'info'">{{ scope.row.status }}</el-tag>
        </template>
      </el-table-column>
      <el-table-column fixed="right" label="操作" width="200">
        <template #default="scope">
          <el-button @click="editAnnouncement(scope.row)" type="text" size="small">编辑</el-button>
          <el-button @click="deleteAnnouncement(scope.row)" type="text" size="small">删除</el-button>
        </template>
      </el-table-column>
    </el-table>

    <!-- 新增/编辑公告对话框 -->
    <el-dialog v-model="dialogVisible" :title="dialogTitle">
      <el-form :model="announcementForm">
        <el-form-item label="公告标题">
          <el-input v-model="announcementForm.title"></el-input>
        </el-form-item>
        <el-form-item label="公告内容">
          <el-input v-model="announcementForm.content" type="textarea" :rows="4"></el-input>
        </el-form-item>
        <el-form-item label="发布时间">
          <el-date-picker v-model="announcementForm.publishTime" type="datetime" placeholder="选择发布时间"></el-date-picker>
        </el-form-item>
      </el-form>
      <template #footer>
        <span class="dialog-footer">
          <el-button @click="dialogVisible = false">取 消</el-button>
          <el-button type="primary" @click="saveAnnouncement">确 定</el-button>
        </span>
      </template>
    </el-dialog>
  </div>
</template>

<script setup>
import { ref } from 'vue';
import { ElMessage, ElMessageBox } from 'element-plus';

const title = ref('');
const dateRange = ref([]);
const tableData = ref([
  {
    id: 1,
    title: '公告1',
    content: '这是公告1的内容。',
    publishTime: '2023-10-01 10:00:00',
    status: 'published'
  },
  // 其他公告数据...
]);

const dialogVisible = ref(false);
const dialogTitle = ref('新增公告');
const announcementForm = ref({
  id: null,
  title: '',
  content: '',
  publishTime: ''
});

const showAddDialog = () => {
  dialogTitle.value = '新增公告';
  announcementForm.value = {
    id: null,
    title: '',
    content: '',
    publishTime: ''
  };
  dialogVisible.value = true;
};

const editAnnouncement = (row) => {
  dialogTitle.value = '编辑公告';
  announcementForm.value = { ...row };
  dialogVisible.value = true;
};

const deleteAnnouncement = (row) => {
  ElMessageBox.confirm(`确定要删除公告《${row.title}》吗？`, '提示').then(() => {
    // 删除逻辑
    ElMessage.success('删除成功');
  });
};

const onSubmit = () => {
  console.log('查询条件:', title.value, dateRange.value);
};

const saveAnnouncement = () => {
  if (announcementForm.value.id) {
    // 编辑逻辑
    ElMessage.success('编辑成功');
  } else {
    // 新增逻辑
    ElMessage.success('新增成功');
  }
  dialogVisible.value = false;
};
</script>

<style scoped>
.announcements-manage {
  padding: 20px;
}

.actions {
  margin-bottom: 20px;
}
</style>